package com.shuhe.handler;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.shuhe.common.EncryptUtils;
import com.shuhe.common.HttpClient;
import com.shuhe.domain.mock.*;
import com.shuhe.metastore.MetaMysql;
import com.shuhe.metastore.Neo4jDB;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Enumeration;

import static com.shuhe.common.CommonFunction.*;


public class MockHandler extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
//            String bodyStr = readAsChars(req);
//            JSONObject bodyJson = JSONObject.parseObject(bodyStr);


            Enumeration<String> headerNames = req.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();

                String value = req.getHeader(key);
                System.out.println(key + "--" + value);
            }


            resp = setResponse(resp);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json");

            String resultstr = "";
            if (req.getRequestURI().endsWith("jczzpt")) {
                resultstr = makeJczzpt();
            }

            if (req.getRequestURI().endsWith("test")) {
                Neo4jDB.makeData("zy.import_ry_1704595499869");
                resultstr = "ok";
            }

            if (req.getRequestURI().endsWith("/cloud/deploy/v1.0/packages")) {
                resultstr = "{\n" +
                        "    \"msg\": \"\",\n" +
                        "    \"code\": \"\",\n" +
                        "    \"data\": true,\n" +
                        "    \"status\": true,\n" +
                        "    \"total\": 1\n" +
                        "}";
            }
            if (req.getRequestURI().endsWith("/cloud/hcr/v1.0/images/load")) {
                resultstr = "{\n" +
                        "    \"code\": \"img.0\",\n" +
                        "    \"status\": true\n" +
                        "}";
            }

            if (req.getRequestURI().endsWith("/api/product/token")) {
                YptSjfwToken token = new YptSjfwToken();
                token.setData("123456");
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/api/product/subscription")) {
                YptSjfwSubscription sub = new YptSjfwSubscription();
                sub.setCode(200);
                sub.setMsg("success");
                YptSjfwSubscriptionData data = new YptSjfwSubscriptionData();
                data.setSubId("id-123");
                data.setSubKey("key-234");
                sub.setData(data);
                resultstr = JSONObject.toJSONString(sub);
            }
            if (req.getRequestURI().endsWith("/api/product/deSubscription")) {
                YptSjfwSubscription sub = new YptSjfwSubscription();
                sub.setCode(200);
                sub.setMsg("success");
                resultstr = JSONObject.toJSONString(sub);
            }
            if (req.getRequestURI().endsWith("/api/saas/app/subscription")) {
                String bodyStr = readAsChars(req);
                JSONObject bodyJson = JSONObject.parseObject(bodyStr);
                String userName = bodyJson.getString("userName");
                String nickName = bodyJson.getString("nickName");
                String password = EncryptUtils.encrypt("yzsw2023", "jmpt2023");
                Connection metaConn = null;
                try {
                    metaConn = MetaMysql.connect();
                    String sql = "delete from sys_user where login_name = '" + userName + "'";
                    MetaMysql.execSql(metaConn, sql);

                    sql = "insert into sys_user(user_id, login_name, user_name, password) " +
                            "values('" + userName + "', '" + userName + "', '" + nickName
                            + "', 'sBaYTBMfxEBrdRYTh0UGEQ==');";
                    MetaMysql.execSql(metaConn, sql);

                    sql = "delete from sys_user_role where user_id = '" + userName + "'";
                    MetaMysql.execSql(metaConn, sql);

                    sql = "insert into sys_user_role(user_id, role_id) values('" + userName + "', '2')";
                    MetaMysql.execSql(metaConn, sql);
                } catch (Exception ex) {
                    YptSjfwSubscription sub = new YptSjfwSubscription();
                    sub.setCode(500);
                    sub.setMsg("fail");
                    resultstr = JSONObject.toJSONString(sub);
                    resp.getWriter().write(resultstr);
                    return;
                } finally {
                    closeDb(metaConn);
                }
                YptSjfwSubscription sub = new YptSjfwSubscription();
                sub.setCode(200);
                sub.setMsg("success");
                resultstr = JSONObject.toJSONString(sub);
                resp.getWriter().write(resultstr);
                return;
            }
            if (req.getRequestURI().endsWith("/api/saas/app/deSubscription")) {
                String bodyStr = readAsChars(req);
                JSONObject bodyJson = JSONObject.parseObject(bodyStr);
                String userName = bodyJson.getString("userName");
                Connection metaConn = null;
                try {
                    metaConn = MetaMysql.connect();
                    String sql = "delete from sys_user where login_name = '" + userName + "'";
                    MetaMysql.execSql(metaConn, sql);

                    sql = "delete from sys_user_role where user_id = '" + userName + "'";
                    MetaMysql.execSql(metaConn, sql);
                } catch (Exception ex) {
                    YptSjfwSubscription sub = new YptSjfwSubscription();
                    sub.setCode(500);
                    sub.setMsg("fail");
                    resultstr = JSONObject.toJSONString(sub);
                    resp.getWriter().write(resultstr);
                    return;
                } finally {
                    closeDb(metaConn);
                }
                YptSjfwSubscription sub = new YptSjfwSubscription();
                sub.setCode(200);
                sub.setMsg("success");
                resultstr = JSONObject.toJSONString(sub);
                resp.getWriter().write(resultstr);
                return;
            }
            if (req.getRequestURI().endsWith("/api/paas/token")) {
                YptSjfwToken token = new YptSjfwToken();
                token.setData("123456");
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/api/paas/subscription")) {
                PassSubscription pass = new PassSubscription();
                pass.setUserName("wyx");
                pass.setPassword("wyx");
                pass.setUrls("127.0.0.1:1234");
                YptSjfwToken token = new YptSjfwToken();
                token.setData(pass);
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/api/paas/deSubscription")) {
                YptSjfwToken token = new YptSjfwToken();
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/api/paas/expansion")) {
                YptSjfwToken token = new YptSjfwToken();
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/api/paas/deExpansion")) {
                YptSjfwToken token = new YptSjfwToken();
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/api/paas/monitor")) {
                PassSubscription pass = new PassSubscription();
                pass.setDbName("testdb1");
                pass.setDbSize("10G");
                pass.setUrls("127.0.0.1:1234");
                YptSjfwToken token = new YptSjfwToken();
                token.setData(pass);
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }

            System.out.println(resultstr);
            resp.getWriter().write(resultstr);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json");
            resp = setResponse(resp);
            String resultstr = "";
            if (req.getRequestURI().endsWith("api/product/list")) {
                YptSjfwList yptSjfwList = new YptSjfwList();

                ArrayList<YptSjfwListData> data = new ArrayList<>();
                data.add(makeYptSjfwListData("1"));
                data.add(makeYptSjfwListData("2"));
                data.add(makeYptSjfwListData("3"));
                data.add(makeYptSjfwListData("4"));
                data.add(makeYptSjfwListData("5"));
                yptSjfwList.setData(data);
                yptSjfwList.setCode(200);
                yptSjfwList.setMsg("success");
                resultstr = JSONObject.toJSONString(yptSjfwList);
            }
            if (req.getRequestURI().endsWith("/sqmtest")) {
                YptSjfwToken token = new YptSjfwToken();
                PassSubscription pass = new PassSubscription();
                pass.setUserName("a");
                token.setData(pass);
                token.setCode(200);
                token.setMsg("success");
                resultstr = JSONObject.toJSONString(token);
            }
            if (req.getRequestURI().endsWith("/sqm/api/api/user/getUserForThird")) {
                String url = "http://172.16.33.1/sqm/api/api/user/getUserForThird?token=" + req.getParameter("token");
                //String url = "http://127.0.0.1:12580/servlet/mockHandler/sqmtest?token=" + req.getParameter("token");
                resultstr = HttpClient.getHttp(url);
            }
            System.out.println(resultstr);
            resp.getWriter().write(resultstr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public YptSjfwListData makeYptSjfwListData(String i) {
        YptSjfwListData yptSjfwListData = new YptSjfwListData();
        YptSjfwListDataApi yptSjfwListDataApi = new YptSjfwListDataApi();
//        YptSjfwListDataApiOperator yptSjfwListDataApiOperator = new YptSjfwListDataApiOperator();
//        yptSjfwListDataApiOperator.setName("ope_name" + i);
//        yptSjfwListDataApiOperator.setPath("/ope_name" + i);
//        yptSjfwListDataApiOperator.setVersion("1.1");
//        yptSjfwListDataApiOperator.setRemark("ope_name-remark" + i);

        yptSjfwListDataApi.setName("api_name" + i);
        yptSjfwListDataApi.setPath("/api_name" + i);
        yptSjfwListDataApi.setVersion("1.1");
        yptSjfwListDataApi.setRemark("api_name-remark" + i);

        yptSjfwListData.setName("data-name" + i);
        yptSjfwListData.setId("123456-1" + i);
        yptSjfwListData.setVersion("1.1");
        yptSjfwListData.setRemark("data-name-remark" + i);
        ArrayList<YptSjfwListDataApiOperator> operatorList = new ArrayList<>();
        //operatorList.add(yptSjfwListDataApiOperator);
        //yptSjfwListDataApi.setOperatorList(operatorList);
        ArrayList<YptSjfwListDataApi> apiList = new ArrayList<>();
        apiList.add(yptSjfwListDataApi);
        yptSjfwListData.setApiList(apiList);
        return yptSjfwListData;
    }

    public String makeJczzpt() {
        JczzptMessage message = new JczzptMessage();
        Jczzpt jczzpt = new Jczzpt();
        jczzpt.setId("1");
        jczzpt.setZdmlid("1301000000000");
        jczzpt.setZdzwm("石家庄支队");
        jczzpt.setSsny("2023-04");
        jczzpt.setBzhdf("1.1");
        jczzpt.setFjgsjdf("2.2");
        jczzpt.setGxzqdf("3.3");
        jczzpt.setCreateTime("2023-04-06 10:00:00");
        jczzpt.setUpdateTime("2023-04-06 10:00:01");
        ArrayList<Jczzpt> data = new ArrayList<>();
        data.add(jczzpt);
        message.setData(data);
        TypeUtils.compatibleWithJavaBean = true;
        String respJson = JSONObject.toJSONString(message);
        return respJson;
    }

    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp = setResponse(resp);
    }
}
